<?php
/**
 * Class for the database entity "user"
 * 
 * @author zonk 
 */

class UserModel {
	
	/**
	 * The user id
	 * @var int
	 */
	private $userid;
	
	/**
	 * The name of the user account
	 * @var string
	 */
	private $name;
	
	/**
	 * The hashed password of the user
	 * @var string
	 */
	private $pw;
	
	/**
	 * The registration date of the user
	 * @var string
	 */
	private $regdate;
	
	/**
	 * The email address of the user
	 * @var string
	 */
	private $mail;
	
	/**
	 * The permission level of the user (2 = Admin, 1 = User, 0 = Guest)
	 * @var int
	 */
	private $level;
	
	/**
	 * The list of movies the user owns
	 * @var ArrayObject
	 */
	private $movies;
	
	/**
	 * The list of recommendations the user has given to movies
	 * @var ArrayObject
	 */
	private $movierecommendations;
	
	/**
	 * The list of recommendations of movies the user has received from other users
	 * @var ArrayObject
	 */
	private $userrecommendations;
	
	/**
	 * The list of storage devices from the user
	 * @var ArrayObject
	 */
	private $storagedevices;
	
	/**
	 * Permissions of this user
	 * @var ArrayObject
	 */
	private $permissions;
	
	/**
	 * @param int $userid;
	 * @param string $name;
	 * @param string $pw;
	 * @param string $regdate;
	 * @param string $mail;
	 * @param int $level;
	 * @param ArrayObject $movies;
	 * @param ArrayObject $movierecommendations;
	 * @param ArrayObject $userrecommendations;
	 * @param ArrayObject $storagedevices;
	 */
	public function __construct($userid, $name, $pw, $regdate, $mail, $level, $movies, $movierecommendations, $userrecommendations, $storagedevices) {
		$this->userid = $userid;
		$this->name = $name;
		$this->pw = $pw;
		$this->regdate = $regdate;
		$this->mail = $mail;
		$this->level = $level;
		$this->movies = $movies;
		$this->movierecommendations = $movierecommendations;
		$this->userrecommendations = $userrecommendations;
		$this->storagedevices = $storagedevices;
		if ($level == 2) {
			$this->permissions->append(Permission::ADMIN);
		}
		if ($level >= 1) {
			$this->permissions->append(Permission::USER);
		}
		$this->permissions->append(Permission::GUEST);
	}
	
	/**======================================
	 * Getter & Setter
	 *=======================================
	 */
	//TODO implement util and the on demand loading of the arrayobjects
	/**
	 * 
	 * @return int
	 */
	public function getUserid()
	{
	    return $this->userid;
	}

	/**
	 * 
	 * @param int $userid
	 */
	public function setUserid($userid)
	{
	    $this->userid = $userid;
	}

	/**
	 * 
	 * @return string
	 */
	public function getName()
	{
	    return $this->name;
	}

	/**
	 * 
	 * @param string $name
	 */
	public function setName($name)
	{
	    $this->name = $name;
	}

	/**
	 * 
	 * @return string
	 */
	public function getPw()
	{
	    return $this->pw;
	}

	/**
	 * 
	 * @param string $pw
	 */
	public function setPw($pw)
	{
	    $this->pw = $pw;
	}

	/**
	 * 
	 * @return string
	 */
	public function getRegdate()
	{
	    return $this->regdate;
	}

	/**
	 * 
	 * @param string $regdate
	 */
	public function setRegdate($regdate)
	{
	    $this->regdate = $regdate;
	}

	/**
	 * 
	 * @return string
	 */
	public function getMail()
	{
	    return $this->mail;
	}

	/**
	 * 
	 * @param string $mail
	 */
	public function setMail($mail)
	{
	    $this->mail = $mail;
	}

	/**
	 * 
	 * @return int
	 */
	public function getLevel()
	{
	    return $this->level;
	}

	/**
	 * 
	 * @param int $level
	 */
	public function setLevel($level)
	{
	    $this->level = $level;
	}

	/**
	 * 
	 * @return ArrayObject
	 */
	public function getMovies()
	{
	    return $this->movies;
	}

	/**
	 * 
	 * @param ArrayObject $movies
	 */
	public function setMovies($movies)
	{
	    $this->movies = $movies;
	}

	/**
	 * 
	 * @return ArrayObject
	 */
	public function getMovierecommendations()
	{
	    return $this->movierecommendations;
	}

	/**
	 * 
	 * @param ArrayObject $movierecommendations
	 */
	public function setMovierecommendations($movierecommendations)
	{
	    $this->movierecommendations = $movierecommendations;
	}

	/**
	 * 
	 * @return ArrayObject
	 */
	public function getUserrecommendations()
	{
	    return $this->userrecommendations;
	}

	/**
	 * 
	 * @param ArrayObject $userrecommendations
	 */
	public function setUserrecommendations($userrecommendations)
	{
	    $this->userrecommendations = $userrecommendations;
	}

	/**
	 * 
	 * @return ArrayObject
	 */
	public function getStoragedevices()
	{
	    return $this->storagedevices;
	}

	/**
	 * 
	 * @param ArrayObject $storagedevices
	 */
	public function setStoragedevices($storagedevices)
	{
	    $this->storagedevices = $storagedevices;
	}

	/**
	 * Returns whether the user has the given permission or not
	 * @param int
	 * @return boolean
	 */
	public function hasPermission($permission)
	{
	    $i = $this->permissions->getIterator();
	    while($i->valid()) {
	    	if($i->current() == $permission) {
	    		return true;
	    	}
	    }
	    return false;
	}
}